//版本 version.html
app.controller('versionCtrl', function ($scope, $http, $location) {
  //页面通用初始化
  pageInit('version');
  // 脱机
  $scope.isOffline = isOffline;
  //发送数据
  var sendData = { versions: [] };
  //初始化
  $http.get('/deploy/version/init').then(function (response) {
    if (response.status == 200) {
      var message = response.data;
      if (message.code == 0) {
        var data = message.data;
        $scope.data = data;
        //初始化发送数据格式
        for (var i = 0; i < data.versions.length; i++) {
          var version = {};
          version.index = data.versions[i].index;
          version.installVersions = data[version.index] ? data[version.index] : 0;
          sendData.versions.push(version);
        }
      } else {
        $.showNotice(message.msg, "danger");
      }
    }
  }).catch(function (response) {
    $.showNotice(response.status + " error：" + response.data.message, "danger");
  });
  //选择升级版本
  $scope.change = function (index, installVersion) {
    for (var i = 0; i < sendData.versions.length; i++)
      if (sendData.versions[i].index == index) sendData.versions[i].installVersions = installVersion;
    //得到平台版本
    $http.post('/deploy/version/common', { 'versions': sendData.versions }).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          var data = message.data;
          $scope.platformVersion = data.installVersions;
          //修改平台版本 index==5
          for (var i = 0; i < sendData.versions.length; i++)
            if (sendData.versions[i].index == 5) sendData.versions[i].installVersions = data.installVersions;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };
  //只有平台的情况下
  $scope.changeOnly = function (installVersion) {
    sendData.versions[0].installVersions = installVersion;
  };
  //提交
  $scope.submit = function (implementer) {
    //验证版本错误情况
    if (sendData.versions.length == 0) {
      $.showNotice("出现错误，无法进行下一步操作", "danger");
      return;
    }
    //验证实施人员
    if (!implementer) {
      $.showNotice("实施人员不能为空", "danger");
      return;
    }
    //验证版本选择
    for (var i = 0; i < sendData.versions.length; i++)
      if (sendData.versions[i].installVersions == 0) {
        $.showNotice("请正确选择版本，保证所有产品版本正确", "danger");
        return;
      }
    //提交
    sendData.implementer = implementer;
    if ($scope.loadingNext) return;
    $scope.loadingNext = true;
    $http.post('/deploy/version/submit', sendData).then(function (response) {
      $scope.loadingNext = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $location.path('module');
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loadingNext = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }
});